function nav(url, types, data) {
  data = data || '';
  if (data == '') {

    if (types == 'n') {
      wx.navigateTo({
        url: url
      })
    } else if (types == 'd') {
      wx.redirectTo({
        url: url,
      })
    } else if (types == 's') {
      wx.switchTab({
        url: url
      })
    } else if (types == 'ln') {
      if (loginFirst()) {
        wx.navigateTo({
          url: url
        })
      }

    } else if (types == 'ld') {
      if (loginFirst()) {
        wx.redirectTo({
          url: url,
        })
      }
    }
  } else {

    var param;
    var num = 0;
    for (var obj in data) {
      if (num == 0) {
        param = obj + '=' + data[obj];
      } else {
        param = param + '&' + obj + '=' + data[obj];
      }
      num++;
    }
    console.log(param)

    if (types == 'n') {
      wx.navigateTo({
        url: url + '?' + param
      })
    } else if (types == 'd') {
      wx.redirectTo({
        url: url + '?' + param
      })
    } else if (types == 's') {
      wx.switchTab({
        url: url + '?' + param
      })
    } else if (types == 'ln') {
      if (loginFirst()) {
        wx.navigateTo({
          url: url + '?' + param
        })
      }

    } else if (types == 'ld') {
      if (loginFirst()) {
        wx.redirectTo({
          url: url + '?' + param
        })
      }
    }
  }


}


function login(num, fn) {
  wx.login({
    success: function (e) {
      var path = 'Personal/login';
      var data = {
        code: e.code
      }
      console.log(data)
      request(path, data, false, (res) => {
        if (res.data.status == 'Success') {
          getUserInfo(res.data.trd_session, num, fn);
        } else if (res.data.status == 'Fail') {
          wx.showModal({
            title: '登录失败',
            content: '服务器繁忙，请稍后再试！',
            showCancel: false
          })
        }
      });
    },
    fail: function (e) {
      console.log(e)

      wx.showModal({
        title: '提示',
        content: '登陆失败，请检查网络并重试！',
        showCancel: false
      })
    },

  })
}

function getUserInfo(trd_session, num, fn) {
  var that = this;
  wx.getUserInfo({
    success: function (res) {
      var path = 'Personal/setUserInfo';

      if (num == '') {
        var data = {
          rawData: res.rawData,
          signature: res.signature,
          encryptedData: res.encryptedData,
          iv: res.iv,
          trd_session: trd_session
        }
      } else {
        var data = {
          rawData: res.rawData,
          signature: res.signature,
          encryptedData: res.encryptedData,
          iv: res.iv,
          trd_session: trd_session,
          phonum: num
        }
      }
      request(path, data, false, (res) => {
        if (res.data.status == "success") {
          fn(res)
        } else {
          console.log(res)
          wx.showModal({
            title: '登陆失败',
            content: '服务器繁忙，请稍后重试！',
            showCancel: false
          })
        }
      })
    },
    fail: function () {
      wx.showModal({
        title: '登陆失败',
        content: '系统繁忙，请稍后重试！',
        showCancel: false
      })
    }
  })
}


function toast(types, content, title, duration, fn) {
  title = title || '提示';
  duration = duration || 2000;
  content = content || '加载中';
  fn = fn || '';
  if (title == 's') {
    title = 'success';
  } else if (title == 'l') {
    title = 'loading';
  }

  if (types == 't') {
    wx.showToast({
      title: content,
      icon: title,
      duration: duration
    })
  } else if (types == 'l') {
    wx.showLoading({
      title: content,
    })
  } else if (types == 'm') {
    if (fn == '') {

      wx.showModal({
        title: title,
        content: content,
        showCancel: true
      })
    } else {
      wx.showModal({
        title: title,
        content: content,
        success: function (res) {
          if (res.confirm) {
            fn(res.confirm);
          } else if (res.cancel) {
            fn(!(res.cancel));
          }
        }
      })
    }

  }

}

function request(path, data, islogin, fn) {
  var url = apps.globalData.adminUrl + path;
  var trd_session = apps.globalData.openUserInfo;
  data.appkey = apps.globalData.appKey;
  if (islogin) {
    if (loginFirst()) {
      data.trd_session = trd_session;
      wx.request({
        url: url,
        data: data,
        method: 'GET',
        success: function (res) {
          getResLogin(res.data);
          fn(res);
        },
        fail: function (res) {

          wx.showModal({
            title: '服务器繁忙',
            content: '服务器繁忙或网络不稳定，请稍后再试！',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/Personal/index/index'
                })
              }
            }
          })
        },
      })
    }
  } else {
    wx.request({
      url: url,
      data: data,
      method: 'GET',
      success: function (res) {
        getRes(res.data);
        fn(res);
      },
      fail: function (res) {

        wx.showModal({
          title: '服务器繁忙',
          content: '服务器繁忙或网络不稳定，请稍后再试！',
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/Personal/index/index'
              })
            }
          }
        })
      },
    })
  }
}







function overload(that, width) {
  width = width || 120;
  that.setData({
    item: {
      dispaly: 'block',
      src: '/images/loading.gif',
      width: width + 'rpx'
    }
  })
}


function overhide(that) {
  that.setData({
    item: {
      dispaly: 'none'
    }
  })
}


function imgtext(that, content) {
  content = content || '';
  if (content == '') {
    var former = {
      goods_intro: [{
        types: 'text',
        info: '正在加载中…'
      }]
    };
    that.setData({
      content: former
    })
  }


}

function sysinfos() {
  return 'https://fuwu.hixiaochengxu.com/Home/';
}

module.exports.sys = sysinfos;

function loginOut() {
  apps.globalData.openUserInfo = '';
  apps.globalData.user_openinfo = '';
  try {
    wx.removeStorageSync('local_user_session')
  } catch (e) {

  }
  wx.showToast({
    title: '退出成功',
    icon: 'success',
    duration: 1000
  })
  return true;
}

function loginExpired() {
  apps.globalData.openUserInfo = '';
  try {
    wx.removeStorageSync('local_user_session')
  } catch (e) {

  }
  wx.showModal({
    title: '登录失效',
    content: '请重新登录',
    showCancel: false,
    success: function (res) {
      if (res.confirm) {
        wx.switchTab({
          url: '/pages/Personal/index/index'
        })
      }
    }
  })
}


function loginFirst() {
  if (apps.globalData.openUserInfo == '') {
    wx.showModal({
      title: '未登录',
      content: '请您先登录',
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '/pages/Personal/index/index'
          })
        }
      }
    })
    return false;
  } else {
    return true;
  }

}


function getRes(res) {
  if (res.state == 'ERROR') {
    wx.navigateTo({
      url: '/pages/Home/error/error?title=小程序出错啦！&content=请联系小程序运营者！'
    })
    return;
  } else if (res.state == 'TIMEOUT') {
    wx.navigateTo({
      url: '/pages/Home/error/error?title=小程序出错啦！&content=请您联系小程序运营者！'
    })
    return;
  } else {

  }
}


function getResLogin(res) {
  if (res.state == 'ERROR') {
    wx.navigateTo({
      url: '/pages/Home/error/error?title=小程序出错啦！&content=请联系小程序运营者！'
    })
    return;
  } else if (res.state == 'TIMEOUT') {
    wx.navigateTo({
      url: '/pages/Home/error/error?title=小程序出错啦！&content=请您联系小程序运营者！'
    })
    return;
  } else if (res.state == 'Login_Expired') {
    loginExpired();
    return;
  } else {

  }
}